package com.crazy.boot.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.crazy.boot.entity.User;
import com.crazy.boot.service.UserService;
import com.crazy.boot.util.ResultUtils;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@GetMapping("/getUserByName")
	public ResultUtils getUserByName(String name, String password) {
		User user = userService.getUserByName(name);
		if(user!=null) {
			return ResultUtils.toClient("0", "success", 0, user);
		}else {
			return ResultUtils.toClient("0", "fail", 0, user);
		}
	}
	
	
	@GetMapping("/listUser")
	@RequiresRoles(value = {"admin"})
	public ResultUtils listUser() {
		List<User> uList =  userService.listUser();
		if(uList.size() > 0) {
			return ResultUtils.toClient("0", "success", uList.size(), uList);
		}else {
			return ResultUtils.toClient("0", "fail", 0, uList);
		}
	}
}
